home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / makedir.cmm < prev    next >
Encoding:
Text File  |  1995-10-06  |  3.7 KB  |  116 lines

  1. // MakeDir.cmm - CEnvi tool to make a full directory path, given any
  2. // ver.1         relative path.  This goes down the directory tree
  3. //               to create any path necessary.  Returns code 1 if path
  4. //               could not be created, or 0 if path was fully created
  5. //               or already exists.
  6.  
  7. usage()
  8. {
  9.    puts(`MakeDir.cmm - Make full directory path`)
  10.    puts("\a")
  11.    puts(`USAGE: CEnvi MakeDir <PathSpec>`)
  12.    puts(``)
  13.    puts(`EXAMPLES: CEnvi MakeDir C:\GOOBER\FOOBER\DOOBER`)
  14.    puts(`          CEnvi MakeDir ..\BOMP\SHOO\BOMP`)
  15.    puts(``);
  16.    exit(EXIT_FAILURE);
  17. }
  18.  
  19. main(argc,argv)
  20. {
  21.   if( argc!=2 ) usage();
  22.   if( !strcmp(argv[1],"/?") ) usage();
  23.  
  24.   if ( MakeFullDirectoryPath(argv[1]) )
  25.     return EXIT_SUCCESS;
  26.    
  27.    // if made it to here then it failed somewhere
  28.    puts("Path not made\a");
  29.    if ( defined(_WINDOWS_) || defined(_NTWIN_)) && !defined(_SHELL_) {
  30.       printf("press any key to exit...");
  31.       getch();
  32.    }
  33.   if( defined(_NWNLM_) ) PressAnyKeyToContinue();
  34.  
  35.   return ( EXIT_FAILURE );
  36. }
  37.  
  38.  
  39. MakeFullDirectoryPath(pPathSpec)
  40.    // make full directories given partial specification.  Return True if
  41.    // directory already exists or can be made, else False if it cannot be
  42.    // made, is invalid, contains wildcards, or any other problem
  43. {
  44.    // failure if any wildcards in the name
  45.    if ( strchr(pPathSpec,'*')  ||  strchr(pPathSpec,'?') )
  46.       return False;
  47.  
  48.    // make full path specification
  49.    if ( !(lFullName = FullPath(pPathSpec)) )
  50.       return False;
  51.  
  52.    // find the first backslash (error if is not one)
  53.    if ( !(lDirDelimiter = strchr(lFullName,'\\')) )
  54.      if( defined(_NWNLM_) && !(lDirDelimiter = strchr(lFullName,'/')) )
  55.        return False;
  56.  
  57.    // if this is at the end of string then this
  58.    // is the root directory, and so make no sub-directories
  59.    if ( lDirDelimiter[1] ) {
  60.  
  61.       // if the final character is a backslash, and if that is
  62.       // not two backslashes in a row, then remove it.
  63.       // some people will add a final backslash on directories
  64.       lLast = strrchr(lDirDelimiter+1,'\\');
  65.       if ( lLast  &&  !lLast[1]  &&  '\\' != lLast[-1] )
  66.          lLast[0] = 0;
  67.  
  68.       if( defined(_NWNLM_) )
  69.         {
  70.           lLast = strrchr(lDirDelimiter+1,'/');
  71.           if ( lLast  &&  !lLast[1]  &&  '/' != lLast[-1] )
  72.             lLast[0] = 0;
  73.         }
  74.       // now make the directories one at a time, not bothering to check
  75.       // if they already exist, because there's no harm making one that
  76.       // already exists
  77.       old = lDirDelimiter;
  78.       while ( lDirDelimiter = strchr(lDirDelimiter+1,'\\') ) {
  79.          lDirDelimiter[0] = 0;
  80.          MakeDirectory(lFullName);
  81.          lDirDelimiter[0] = '\\';
  82.       }
  83.       lDirDelimiter = old;
  84.       while( defined(_NWNLM_) && (lDirDelimiter = strchr(lDirDelimiter+1,'/')) )
  85.         {
  86.           lDirDelimiter[0] = 0;
  87.           MakeDirectory(lFullName);
  88.           lDirDelimiter[0] = '/';
  89.         }
  90.       MakeDirectory(lFullName);
  91.    }
  92.  
  93.    // finally, to see if it all worked, return whether dir exists
  94.    return ( NULL != Directory(lFullName,False,FATTR_SUBDIR,FATTR_SUBDIR) );
  95. }
  96.  
  97. MakeDirectory(pDirName) // no return value
  98. {
  99.    if defined(_OS2_) {
  100.       #define ORD_DOS32CREATEDIR 270
  101.       DynamicLink("doscalls",ORD_DOS32CREATEDIR,BIT32,CDECL,pDirName,0)
  102.    } else if ( defined(_NTCON_) || defined(_NTWIN_) ) {
  103.       DynamicLink("KERNEL32","CreateDirectoryA",STDCALL,pDirName,NULL);
  104.    } else if( defined(_NWNLM_) )
  105.    {
  106.      mkdir(pDirName);
  107.    } else { // dos or windows use the same code
  108.     lReg.ah = 0x39;
  109.     if !defined(_DOS32_)
  110.        lReg.ds = segment(pDirName), lREg.dx = offset(pDirName);
  111.     else
  112.        lReg.dx = pointer(pDirName);
  113.     interrupt(0x21,lReg);
  114.    }
  115. }
  116.